package io.gitee.zhangbinhub.admin.controller.openinner

import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.api.CommonPath
import io.gitee.zhangbinhub.admin.api.ServerApi
import io.gitee.zhangbinhub.admin.base.BaseController
import io.gitee.zhangbinhub.admin.entity.Organization
import io.gitee.zhangbinhub.admin.service.OrganizationService
import io.gitee.zhangbinhub.admin.vo.OrganizationVo
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * @since JDK 11
 */
@Validated
@RestController
@RequestMapping(CommonPath.openInnerBasePath)
@Tag(name = "机构信息（内部开放接口）")
class OpenInnerOrgController @Autowired
constructor(
    logAdapter: LogAdapter,
    private val organizationService: OrganizationService
) : BaseController(logAdapter) {
    private fun listToVo(organizationList: List<Organization>): List<OrganizationVo> =
        organizationList.map { organization ->
            OrganizationVo().apply {
                BeanUtils.copyProperties(organization, this)
            }
        }

    @Operation(summary = "获取机构列表", description = "查询所有机构列表")
    @GetMapping(value = [ServerApi.orgConfig], produces = [MediaType.APPLICATION_JSON_VALUE])
    fun orgList(): ResponseEntity<List<OrganizationVo>> =
        ResponseEntity.ok(listToVo(organizationService.getAllOrgList()))
}